home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Hyper / Ss-Sz / stripDelim.cpt / stripDelim.a next >
Text File  |  1987-11-27  |  10KB  |  385 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;
  3. ;    XFNC    1385        stripDelim (T, D, C, X)
  4. ;
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  6. ;
  7. ;    © 1987 Theodore S. Romano DMD
  8. ;
  9. ;   If you want to see more, send me what you think it's worth:
  10. ;    I would rather push keys then pick plaque — TSR
  11. ;
  12. ;    1413 Palisades Beach Rd.
  13. ;    Peoples Republic of Santa Monica, CA 90401
  14. ;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16. ;
  17. ; processes the text in T under control of D, C, and X as described below
  18. ;
  19. ; T is the text to process.
  20. ; D is the list of delimiters
  21. ; C is a conversion control code
  22. ; X is a list of characters to remove
  23. ;
  24. ; D, C, and X are optional arguments.
  25. ; D defaults to:
  26. ;        space   comma   tab   return
  27. ;
  28. ; C defaults to empty, as does X.
  29. ;
  30. ; All occurances of any of the delimiters (D) are converted to occurances
  31. ; of the first delimiter in the list.
  32. ;
  33. ; Contiguous sequences of delimiters are converted to a single
  34. ; delimiter (the first one in the list).
  35. ;
  36. ; Leading and trailing delimiters are removed.
  37. ;
  38. ; The conversion control code (C) can be empty, in which case no
  39. ; conversion occurs, or it may begin with a conversion control code
  40. ; from the table below, followed by a colon.
  41. ;
  42. ; Any remaining characters in the conversion control code parameter
  43. ; must occur in pairs, the first being mapped onto the second.
  44. ;
  45. ; If the list of characters to remove (X) is not empty then those
  46. ; characters are deleted as they are encountered and are not
  47. ; copied to the result string.
  48. ;
  49. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  50. ;
  51. ; COMPILE:
  52. ;
  53. ;    asm stripDelim.a
  54. ;    link -sn Main=stripDelim -sn STDIO=stripDelim ∂
  55. ;        -sn INTENV=stripDelim -rt XFCN=1385 ∂
  56. ;        stripDelim.a.o -o "HD:Home"
  57. ;
  58. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  59. ;
  60.             INCLUDE    'Traps.a'
  61.             INCLUDE    'ToolEqu.a'
  62.             INCLUDE    'QuickEqu.a'
  63.             INCLUDE    'SysErr.a'
  64.             INCLUDE    'SysEqu.a'
  65.             INCLUDE    'HyperXCmd.a'
  66.  
  67.             STRING    ASIS
  68.             CASE    OBJECT
  69.  
  70.             EXPORT    stripDelim
  71. stripDelim    PROC
  72. ;
  73. ; make room for the mapping table and save the registers we'll need
  74. ; our caller preserves registers D3-D7/A1-A5 for us
  75. ;
  76.             LINK    A6,#-256            ; character mapping table
  77.             MOVEM.L    D1/D2,-(SP)            ; save registers
  78. ;
  79. ; check arguments
  80. ;
  81.             MOVE.L    8(A6),A1            ; XCmdBlockPtr
  82.             MOVE.L    params(A1),A0        ; handle to source text
  83. ;
  84. ; make sure there is some text to process and allocate space for results
  85. ;
  86.             _GetHandleSize                ; for clone to hold result
  87.             TST.L    D0
  88.             BEQ        done                ; empty source
  89.             _NewHandle                    ; allocate place for result
  90.             BNE        done
  91.             MOVE.L    8(A6),A1            ; XCmdBlockPtr
  92.             MOVE.L    A0,returnValue(A1)    ; save handle to result            
  93. ;
  94. ; flesh out mapping table, map each character onto itself
  95. ;
  96.             MOVE.L    #255,D0
  97.             MOVE.L    A6,A0
  98. mapFill:    MOVE.B    D0,-(A0)
  99.             DBRA    D0,mapFill
  100. ;
  101. ; setup case and character conversions
  102. ;
  103. noRemovals:    CMP.W    #3,(A1)                ; have 3rd parameter ?
  104.             BLT        doDelims
  105.             MOVE.L    params+8(A1),A2        ; conversion list handle
  106.             MOVE.L    (A2),A3
  107.             CLR.L    D0
  108. nextCode:    MOVE.B  (A3)+,D0
  109.             BEQ        chkDelete            ; empty string
  110.             CMP.B    #':',D0
  111.             BEQ        doMap
  112.             SUB.B    #'A',D0
  113.             BGE.S    @1
  114.             BRA.S    nextCode
  115. @1:            CMP.B    #'W'-'A',D0
  116.             BGT.S    nextCode
  117. ;
  118. ; we get here if we are likely to have a conversion code
  119. ;
  120.             ADD.L    D0,D0
  121.             MOVE.W    base(D0),D1
  122.             JMP        base(D1.W)
  123. base:        DC.W    cA-base                ; A -- alpha, lower case letters
  124.             DC.W    @none-base            ; B
  125.             DC.W    @none-base            ; C
  126.             DC.W    cD-base                ; D -- decimal, + - .
  127.             DC.W    @none-base            ; E
  128.             DC.W    cF-base                ; F -- flip (toggle state of entries)
  129.             DC.W    cG-base                ; G -- garbage (non printing)
  130.             DC.W    @none-base            ; H
  131.             DC.W    @none-base            ; I
  132.             DC.W    @none-base            ; J
  133.             DC.W    @none-base            ; K
  134.             DC.W    cL-base                ; L -- lower case (convert upper to)
  135.             DC.W    cM-base                ; M -- mac symbols
  136.             DC.W    cN-base                ; N -- numerals
  137.             DC.W    @none-base            ; O
  138.             DC.W    cP-base                ; P -- punctuation
  139.             DC.W    @none-base            ; Q
  140.             DC.W    @none-base            ; R
  141.             DC.W    cS-base                ; S -- symbols
  142.             DC.W    cT-base                ; T -- text only
  143.             DC.W    cU-base                ; U -- upper case (lower to upper)
  144.             DC.W    cV-base                ; V -- values (decimal & numerals)
  145.             DC.W    cW-base                ; W -- white space
  146. @none        BRA.S    nextCode
  147. ;
  148. ; A (alpha) delete the lowercase letters
  149. ;
  150. cA:            MOVE.L    #'z'-'a',D1            ; from a to z
  151.             LEA        'a'(A0),A2
  152. @1:            CLR.B    (A2)+
  153.             DBRA    D1,@1
  154.             BRA.S    nextCode
  155. ;
  156. ; D (decimal) delete the . - , and + symbols from the table
  157. ;
  158. cD:            CLR.L    '+'(A0)
  159.             BRA.S    nextCode
  160. ;
  161. ; F (flip table) flip the table entries
  162. ;
  163. cF:            MOVE.W    #255,D1
  164.             MOVE.L    A6,A2
  165. @1:            MOVE.B    -(A2),D0
  166.             BEQ.S    @2
  167.             CLR.B    (A2)
  168.             BRA.S    @3
  169. @2:            MOVE.B    D1,(A2)
  170. @3:            DBRA    D1,@1
  171.             CLR.B    (A0)
  172.             BRA        nextCode
  173. ;
  174. ; G (garbage) delete the non-printing characters
  175. ;
  176. cG:            CLR.B    $7F(A0)                ; DEL            
  177.             MOVE.L    #$07-$01,D1            ; from SO to US
  178.             LEA        $01(A0),A2
  179. @1:            CLR.B    (A2)+
  180.             DBRA    D1,@1
  181.             MOVE.L    #$1F-$0E,D1            ; from SO to US
  182.             LEA        $0E(A0),A2
  183. @2:            CLR.B    (A2)+
  184.             DBRA    D1,@2
  185.             MOVE.L    #$FF-$D9,D1            ; from char D9 to FF
  186.             LEA        $D9(A0),A2            ; first char to zap
  187. @3:            CLR.B    (A2)+
  188.             DBRA    D1,@3
  189.             BRA        nextCode
  190. ;
  191. ; L (lower case) convert the uppercase letters to lowercase
  192. ;
  193. cL:            LEA        'A'(A0),A2            ; pointer to table entry for 'A'
  194.             MOVE.B    #'a'-'A',D0            ; what to add from each entry
  195.             MOVE.L    #25,D1                ; DBRA style count
  196. @1:            ADD.B    D0,(A2)+            ; convert entry to uppercase
  197.             DBRA    D1,@1                ; loop
  198.             BRA        nextCode
  199. ;
  200. ; M (macintosh characters) delete the macintosh non ascii characters
  201. ;
  202. cM:            MOVE.L    #$D8-$80,D1            ; from fishy A to fishy Y
  203.             LEA        $80(A0),A2
  204. @1:            CLR.B    (A2)+
  205.             DBRA    D1,@1
  206.             BRA        nextCode
  207. ;
  208. ; N (numbers) delete the digits 0 to 9
  209. ;
  210. cN:            MOVE.L    #'9'-'0',D1            ; from 0 to 9
  211.             LEA        '0'(A0),A2
  212. @1:            CLR.B    (A2)+
  213.             DBRA    D1,@1
  214.             BRA        nextCode
  215. ;
  216. ; P (punctuation) delete ! " ' , . / : ; ? _
  217. ;
  218. cP:            LEA        punctuation,A2        ; string with punctuation characters
  219.             MOVE.L    #11,D1
  220. @1:            MOVE.B    (A2)+,D0
  221.             CLR.B    0(A0,D0)
  222.             DBRA    D1,@1
  223.             BRA        nextCode
  224. ;
  225. ; S (symbols) delete rest of standard symbols (not P or D)
  226. ;
  227. cS:            LEA        symbols,A2            ; string with symbol characters
  228.             MOVE.L    #17,D1
  229. @1:            MOVE.B    (A2)+,D0
  230.             CLR.B    0(A0,D0)
  231.             DBRA    D1,@1
  232.             BRA        nextCode
  233. ;
  234. ; T (text only) delete everything but alphanumeric and punctuation
  235. ;
  236. cT:            LEA        1(A0),A2            ; pointer to table entrys
  237.             MOVE.L    #'/'-1,D1            ; dbra count
  238. @1:            CLR.B    (A2)+                ; clear entry
  239.             DBRA    D1,@1                ; loop
  240.             CLR.W    '<'(A0)                ; < =
  241.             CLR.B    '>'(A0)                ; >
  242.             CLR.B    '^'(A0)                ; ^
  243.             CLR.B    '`'(A0)                ; `
  244.             LEA        '{'(A0),A2
  245.             MOVE.L    #255-'{',D1
  246. @2:            CLR.B    (A2)+
  247.             DBRA    D1,@2
  248.             LEA        punctuation,A2        ; string with punctuation characters
  249.             MOVE.L    #11,D1
  250. @3:            MOVE.B    (A2)+,D0
  251.             MOVE.B    D0,0(A0,D0)
  252.             DBRA    D1,@3
  253.             BRA        nextCode
  254. ;
  255. ; U (upper case) convert the lower case letters to upper case
  256. ;
  257. cU:            LEA        'a'(A0),A2            ; pointer to table entry for 'a'
  258.             MOVE.L    #'a'-'A',D0            ; what to subtract from each entry
  259.             MOVE.L    #25,D1                ; DBRA style count
  260. @1:            SUB.B    D0,(A2)+            ; convert entry to uppercase
  261.             DBRA    D1,@1                ; loop
  262.             BRA        nextCode
  263. ;
  264. ; V (value only) delete everything but numeric and decimal
  265. ;
  266. cV:            LEA        1(A0),A2            ; pointer to table entrys
  267.             MOVE.L    #'/'-1,D1            ; dbra count
  268. @1:            CLR.B    (A2)+                ; clear entry
  269.             DBRA    D1,@1                ; loop
  270.             LEA        ':'(A0),A2
  271.             MOVE.L    #255-':',D1
  272. @2:            CLR.B    (A2)+
  273.             DBRA    D1,@2
  274.             MOVE.L    #'+,-.','+'(A0)
  275.             BRA        nextCode
  276. ;
  277. ; W (white space) delete white space characters
  278. ;
  279. cW:            MOVE.L    #5,D1                ; from BS to CR
  280.             LEA        8(A0),A2
  281. @1:            CLR.B    (A2)+
  282.             DBRA    D1,@1
  283.             CLR.B    ' '(A0)
  284.             BRA        nextCode
  285. ;
  286. ; setup character mapping, the remaining characters in the
  287. ; conversion string are paired.  The first should be set to change
  288. ; into the second
  289. ;
  290. doMap:        CLR.L    D0
  291.             CLR.L    D1
  292. @next:        MOVE.B    (A3)+,D0
  293.             BEQ.S    chkDelete
  294.             MOVE.B    (A3)+,D1
  295.             BEQ.S    chkDelete
  296.             MOVE.B    D1,0(A0,D0)
  297.             BRA.S    @next
  298. ;
  299. ; check for optional arguments (deletion list)
  300. ;
  301. chkDelete:    CMP.W    #4,(A1)                ; maximum number of arguments
  302.             BLT.S    doDelims
  303. ;
  304. ; map characters to remove onto nulls
  305. ;
  306.             MOVE.L    params+12(A1),A2    ; removals list
  307.             MOVE.L    (A2),A3
  308.             CLR.L    D0
  309. @next        MOVE.B    (A3)+,D0
  310.             BEQ.S    doDelims
  311.             CLR.B    0(A0,D0)
  312.             BRA.S    @next
  313. ;
  314. ; now setup the list of delimiters
  315. ;
  316. doDelims:    CMP.W    #2,(A1)                ; have 2nd parameter ?
  317.             BLT.S    defDelims
  318.             MOVE.L    params+4(A1),A2        ; handle to delimiters
  319.             MOVE.L    (A2),A3                ; pointer to delimiters
  320.             BRA.S    setDelims
  321. defDelims:    LEA        theDelims,A3
  322. setDelims:    CLR.L    D1
  323.             MOVE.B    (A3),D1                ; primary delimiter
  324.             CLR.L    D0
  325. @next:        MOVE.B    (A3)+,D0            ; next delimiter
  326.             BEQ.S    setupDone
  327.             MOVE.B    D1,0(A0,D0)            ; map it onto first delimiter
  328.             BRA.S    @next
  329. ;
  330. ; finally, we can do what we started to — filtering the source text
  331. ; D1 is the primary delimiter
  332. ; A0 is a pointer to the mapping table
  333. ;
  334. setupDone:    MOVE.L    params(A1),A2        ; handle to source text
  335.             MOVE.L    (A2),A3
  336.             MOVE.L    D1,D2                ; save primary delimiter
  337.             MOVE.L    returnValue(A1),A2    ; handle to result text
  338.             MOVE.L    (A2),A1                ; pointer to space for result string
  339. ;
  340. ; processing loop; fetch the next character and map it
  341. ;
  342. scanLoop:    MOVE.B    (A3)+,D0
  343.             BEQ.S    atNull                ; source ends
  344.             MOVE.B    0(A0,D0),D0            ; map character
  345.             BEQ.S    scanLoop            ; character deleted
  346. ;
  347. ; check for delimiters
  348. ;
  349.             CMP.B    D2,D0                ; is recent character a delimiter
  350.             BNE.S    copy                ; nope, so copy it
  351.             CMP.B    D1,D0                ; yes, so if the previous was also
  352.             BEQ.S    scanLoop            ; then skip it
  353. ;
  354. ; copy the accepted character to the result
  355. ;
  356. copy:        MOVE.B    D0,(A1)+
  357.             MOVE.L    D0,D1
  358.             BRA.S    scanLoop
  359. ;
  360. ; done, delete any trailing delimiter and shrink wrap result
  361. ;
  362. atNull:        MOVE.L    (A2),D0
  363.             CMP.L    D0,A1
  364.             BEQ.S    putResult
  365.             CMP.B    -1(A1),D2            ; was last char a delimiter
  366.             BNE.S    putResult
  367.             SUB.L    #1,A1                ; yes, delete it
  368. putResult:    CLR.B    (A1)+                ; append null to result
  369.             MOVE.L    A1,D0
  370.             SUB.L    (A2),D0                ; compute length to fix handle size
  371.             MOVE.L    A2,A0
  372.             _SetHandleSize
  373. ;
  374. ; all done
  375. ;
  376. done:        MOVEM.L    (SP)+,D1/D2
  377.             UNLK    A6
  378.             MOVEM.L    (SP)+,A0/A1            ; get return address and parameter
  379.             JMP        (A0)                ; return
  380. ;
  381. theDelims    DC.B    $20,$2C,$0D,$09,$00,$00
  382. punctuation    DC.B    '!"'',./:;?_[]'
  383. symbols        DC.B    '#$%&()*<=>@^`{|}\~'
  384.             END
  385.